﻿/*
 * My Timer - frmTray.cs
 * 
 * Copyright (C) 2009 Ahmad Hanis Azmi
 * 
 * This file is part of My Timer
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace My_Timer
{
    public partial class frmTray : Form
    {
        //frmMain r_frmMain = new frmMain();
        public frmTray()
        {
            InitializeComponent();
        }

        public static PlayAudio r_PlayAudio = new PlayAudio();
        private static string _file;
        private static byte _id;

        public static void doPlayAudio(byte id, string file)
        {
            // id
            // 0 = play
            // 1 = close
            _file = file;
            _id = id;

            Program.r_frmTray.backgroundWorkerPlayAudio.RunWorkerAsync();
        }

        private void frmTray_Load(object sender, EventArgs e)
        {
            this.Visible = false;
            this.Hide();

            LoadString.f_LoadStringTray();

            if (Settings.StartMinimized == false)
            {
                GlobalClass.f_chkfrmMain();
                GlobalClass.r_frmMain.Show();
                GlobalClass.r_frmMain.Activate();
            }

            // timer to save Timer.xml every 1 minute
            Timer t_SaveSettings = new Timer();
            t_SaveSettings.Tick += new EventHandler(t_SaveSettings_Tick);
            t_SaveSettings.Interval = 60000;
            t_SaveSettings.Enabled = true;
        }

        private void notifyTrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (GlobalClass.r_frmMain.Created == false || GlobalClass.r_frmMain.Visible == false)
            {
                GlobalClass.f_chkfrmMain();
                GlobalClass.r_frmMain.Show();
                GlobalClass.r_frmMain.Activate();
            }
            else
            {
                GlobalClass.r_frmMain.Close();
                GlobalClass.r_frmMain.Dispose();
            }
        }

        private void toolStripMenuItemShow_Click(object sender, EventArgs e)
        {
            if (GlobalClass.r_frmMain.Created == false || GlobalClass.r_frmMain.Visible == false)
            {
                GlobalClass.f_chkfrmMain();
                GlobalClass.r_frmMain.Show();
                GlobalClass.r_frmMain.Activate();
            }
            else
            {
                GlobalClass.r_frmMain.Close();
                GlobalClass.r_frmMain.Dispose();
            }
        }

        private void toolStripMenuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
            Program.ExitApplication();
        }

        private void contextMenuStripTray_Opening(object sender, CancelEventArgs e)
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GlobalClass.f_chkfrmAbout();
            //GlobalClass.r_frmAbout.Visible = false;

            if (GlobalClass.r_frmAbout.Visible == false)
            {
                GlobalClass.r_frmAbout.Hide();
                GlobalClass.r_frmAbout.ShowDialog(this);
            }

            GlobalClass.r_frmAbout.Activate();
        }

        private void backgroundWorkerPlayAudio_DoWork(object sender, DoWorkEventArgs e)
        {
            // DUMMY HACK: AVOID LAGGING WHEN PLAY AUDIO
            // - open mp3 file first using other thread to avoid lag when playing audio
            //   from main thread
            // - result: main thread open mp3 file faster, audio playback can be stopped
            //   from main thread
            r_PlayAudio.Close();
            if (_id == 0)
            {
                ReadFile(_file);
                r_PlayAudio.Open(_file);
                r_PlayAudio.Play(false);
            }
        }

        private void backgroundWorkerPlayAudio_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            r_PlayAudio.Close();
            if (_id == 0)
            {
                r_PlayAudio.Open(_file);
                r_PlayAudio.Play(false);
            }
        }

        private void t_SaveSettings_Tick(object sender, EventArgs e)
        {
            // save Timer.xml every 1 minute
            SaveSetting.SaveTimerSetting();

            // save others when new features implemented
        }

        public static void ReadFile(string filePath)
        {
            // buffer the file first, then close
            // hopefully this can avoid lag when
            // main thread tries to open the file

            byte[] buffer;
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            try
            {
                int length = (int)fileStream.Length;  // get file length
                buffer = new byte[length];            // create buffer
                int count;                            // actual number of bytes read
                int sum = 0;                          // total number of bytes read

                // read until Read method returns 0 (end of the stream has been reached)
                while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                    sum += count;  // sum is a buffer offset for next reading
            }
            finally
            {
                fileStream.Close();
            }
        }
    }
}
